<?php
// this program will run at 12am everyday
ini_set('display_errors', 1);
error_reporting(E_ALL | E_STRICT);
set_time_limit(60*60);
ini_set('memory_limit', '128M');
date_default_timezone_set('America/New_York');

	require_once "config.php";
	require_once dirname(__FILE__). '/util/connection.class.php';
	require_once dirname(__FILE__). '/util/TimeUtil.php';
	$dbconn = Connection::getInstance();
	
	$sql = 'truncate winners';
	$dbconn->execute($sql);
	$sql = 'select * from sites';
	$sites = $dbconn->query($sql);
	foreach ($sites as $site)
	{
		$sql = 'select firstname, lastname, daily from users where site_id=? and daily>0 order by daily desc limit 10';
		$winners = $dbconn->query($sql, array($site['id']));
		foreach ($winners as $winner)
		{
			$sql = 'insert into winners set site_id=?, user_name=?, won=?';
			$dbconn->execute($sql, array($site['id'], $winner['firstname'].' '.substr($winner['lastname'], 0, 1), $winner['daily']));
		}
	}

	$isAnotherWeek = TimeUtil::isAnotherWeek();
	$isAnotherMonth = TimeUtil::isAnotherMonth();
	$sql = 'update users set daily=0, daily_played=0';
	$dbconn->execute($sql);
	if ($isAnotherWeek)
	{
		$sql = 'update users set weekly=0, weekly_played=0';
		$dbconn->execute($sql);
	}
	if ($isAnotherMonth)
	{
		$sql = 'update users set monthly=0, monthly_played=0';
		$dbconn->execute($sql);
	}
?>